package com.molyfun.parents.activity.report;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.adapter.MainAskPagerAdapter;
import com.molyfun.parents.rest.bean.RepoetFromsLineListResponse;
import com.molyfun.parents.rest.bean.ReportFromsTypeResponse;
import com.molyfun.parents.utils.Const;
import com.molyfun.parents.utils.PreferencesStorageUtil;
import com.molyfun.parents.view.ThreeTabBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class ReportFromsActivity extends BaseActivity implements View.OnClickListener, ThreeTabBar.ThreeBarClickListener, ViewPager.OnPageChangeListener {

    @InjectView(R.id.base_title_left)
    ImageView mTitleBack;
    @InjectView(R.id.report_top_bar)
    ThreeTabBar mThreeTabar;
    @InjectView(R.id.reportFroms_viewpager)
    ViewPager mViewPager;
    @InjectView(R.id.base_title)
    TextView mTitle;

    private MainAskPagerAdapter mMainAskPagerAdapter;

    private List<Fragment> reportFromsFragmentList = null;
    private String mTimeInterval;
    private String mReportId;
    private String courseassistnum;
    private String coursefinishnum;
    private String coursetotal;
    private String uncompletenum;
    private ArrayList<ReportFromsTypeResponse.ReportTypeBean.TypelistBean> typelist;
    private ArrayList<RepoetFromsLineListResponse.LineListBean> lineList;


    public static void startActivity(Context context, String time, String reportId) {
        Intent intent = new Intent();
        intent.setClass(context, ReportFromsActivity.class);
        intent.putExtra(Const.FLAG_TIME_INTERVAL, time);
        intent.putExtra(Const.REPORT_ID, reportId);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report_froms);
        ButterKnife.inject(this);

        getSupportActionBar().setTitle("");
        mTitle.setText("报表统计");

        mThreeTabar.initData("总述", "类型", "课程");
        mTimeInterval = getIntent().getStringExtra(Const.FLAG_TIME_INTERVAL);
        mReportId = getIntent().getStringExtra(Const.REPORT_ID);
        loadData();

    }

    private void initView() {
        reportFromsFragmentList = new ArrayList<>();
        GeneralDescripeFragment generalDescripeFragment = GeneralDescripeFragment.newInstance(ThreeTabBar.ThreeTabChooseTab.BAR_ONE, mTimeInterval, mReportId, coursetotal,  courseassistnum, coursefinishnum,uncompletenum);
        TypeFragment typeFragment = TypeFragment.newInstance(ThreeTabBar.ThreeTabChooseTab.BAR_TWO, mTimeInterval, mReportId, typelist);
        CourseFragment courseFragment = CourseFragment.newInstance(ThreeTabBar.ThreeTabChooseTab.BAR_THREE, mTimeInterval, mReportId,lineList);
        reportFromsFragmentList.add(generalDescripeFragment);
        reportFromsFragmentList.add(typeFragment);
        reportFromsFragmentList.add(courseFragment);
        mThreeTabar.setThreeBarClickListener(this);
        if(reportFromsFragmentList.size()>0) {
            mMainAskPagerAdapter = new MainAskPagerAdapter(getSupportFragmentManager(), reportFromsFragmentList);
            mViewPager.setAdapter(mMainAskPagerAdapter);
        }else {
            return;
        }
        mViewPager.addOnPageChangeListener(this);
        mViewPager.setCurrentItem(0);


        mTitleBack.setOnClickListener(this);

    }

    private void loadData() {
        showProgressDialog();
        Call<ReportFromsTypeResponse> call = MFPApplication.getInstance().getRestClient().getReportType(mReportId);
        call.enqueue(new Callback<ReportFromsTypeResponse>() {
            @Override
            public void onResponse(Call<ReportFromsTypeResponse> call, Response<ReportFromsTypeResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {
                    courseassistnum = response.body().getData().getCourseassistnum();
                    coursefinishnum = response.body().getData().getCoursefinishnum();
                    coursetotal = response.body().getData().getCoursetotal();
                    uncompletenum = response.body().getData().getUncompletenum();
                    typelist = response.body().getData().getTypelist();
//                    initView();
                    getLineData();
                } else {
                    toast(response.body() == null ? "" : response.body().getMsg() == null ? "" : response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<ReportFromsTypeResponse> call, Throwable t) {
                dismissProgressDialog();

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;

        }
    }


    @Override
    public void onThreeBarClick(int chooseTab) {
        if (PreferencesStorageUtil.isUserEnter(this)) {
            switch (mThreeTabar.getCurrentChooseTab()) {
                case ThreeTabBar.ThreeTabChooseTab.BAR_ONE:
                    mViewPager.setCurrentItem(0);
                    break;
                case ThreeTabBar.ThreeTabChooseTab.BAR_TWO:
                    mViewPager.setCurrentItem(1);
                    break;
                case ThreeTabBar.ThreeTabChooseTab.BAR_THREE:
                    mViewPager.setCurrentItem(2);
                    break;

            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            mThreeTabar.setTabClick(ThreeTabBar.ThreeTabChooseTab.BAR_ONE);
        } else if (position == 1) {
            mThreeTabar.setTabClick(ThreeTabBar.ThreeTabChooseTab.BAR_TWO);
        } else {
            mThreeTabar.setTabClick(ThreeTabBar.ThreeTabChooseTab.BAR_THREE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public void getLineData() {
        showProgressDialog();
        Call<RepoetFromsLineListResponse> call = MFPApplication.getInstance().getRestClient().getReportLineDate(mReportId);
        call.enqueue(new Callback<RepoetFromsLineListResponse>() {
            @Override
            public void onResponse(Call<RepoetFromsLineListResponse> call, Response<RepoetFromsLineListResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {
                    lineList = response.body().getData();
                    initView();
//                    getCourseList();
                } else {
                    toast(response.body() == null ? "" : response.body().getMsg() == null ? "" : response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<RepoetFromsLineListResponse> call, Throwable t) {
                dismissProgressDialog();

            }
        });
    }


}
